package com.david.func_test.activity

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.david.func_test.R


class StepActivity: Activity() {
    private var mStepTV: TextView? = null
    private var mSensorManager: SensorManager? = null
    private var mListener: SensorEventListener? = object :SensorEventListener {
        override fun onSensorChanged(event: SensorEvent) {
            println("@@@:" + event.sensor.type + "--" + Sensor.TYPE_STEP_DETECTOR + "--" + Sensor.TYPE_STEP_COUNTER)
            if (event.sensor.type == Sensor.TYPE_STEP_DETECTOR) {
                if (event.values[0] == 1.0f) {
                    this@StepActivity.mStepDetector++
                }
            } else if (event.sensor.type == Sensor.TYPE_STEP_COUNTER) {
                this@StepActivity.mStepCounter = event.values[0].toInt()
            }
            val desc = String.format("设备检测到您当前走了%d步，自开机以来总数为%d步", mStepDetector, mStepCounter)
            mStepTV?.setText(desc)
        }

        override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
    }
    private var mStepDetector = 0 // 自应用运行以来STEP_DETECTOR检测到的步数

    private var mStepCounter = 0 // 自系统开机以来STEP_COUNTER检测到的步数


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_step)
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.ACTIVITY_RECOGNITION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            Log.d("TAG", "PERMISSION 'ACTIVITY_RECOGNITION' NOT GRANTED")
            ActivityCompat.requestPermissions(
                this, arrayOf(Manifest.permission.ACTIVITY_RECOGNITION),
                0
            )
        } else {
            Log.d("TAG", "PERMISSION 'ACTIVITY_RECOGNITION' GRANTED")
        }
        mStepTV = findViewById<View>(R.id.tv_step) as TextView
        mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager


        getSharedPreferences("test", Activity.MODE_PRIVATE).getString("BootUpReceiver", "default")
            ?.let { Log.e("BootUpReceiver", it) }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        Log.e("permission", "requestCode:$requestCode")
    }

    override fun onResume() {
        super.onResume()
        mSensorManager!!.registerListener(
            mListener, mSensorManager!!.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR),
            SensorManager.SENSOR_DELAY_NORMAL
        )
        mSensorManager!!.registerListener(
            mListener, mSensorManager!!.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),
            SensorManager.SENSOR_DELAY_NORMAL
        )
    }

    override fun onPause() {
        super.onPause()
        mSensorManager!!.unregisterListener(mListener)
    }

    override fun onDestroy() {
        super.onDestroy()
        mSensorManager?.unregisterListener(mListener)
    }
}